﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using xyjc;

namespace app
{
    public partial class Form1Config : Form
    {
        private AppConfig appConfig { get; set; }
        private Form1 parentForm { get; set; }
        private Action<AppConfig> okCallback { get; set; }

        public Form1Config(AppConfig config, Form1 parent, Action<AppConfig> callback)
        {
            InitializeComponent();

            appConfig = config;
            parentForm = parent;
            okCallback = callback;

            string[] files = Directory.GetFiles(appConfig.TemplatePath, "*.doc*", SearchOption.AllDirectories);
            foreach(string file in files)
            {
                selectTemplateFileComboBox.Items.Add(file);
            }

            if (appConfig.TemplateFile != null) selectTemplateFileComboBox.Text = appConfig.TemplateFile;
            if (appConfig.ListenPath != null) listenPathTextBox.Text = appConfig.ListenPath;
            if (appConfig.OutputPath != null) outputPathTextBox.Text = appConfig.OutputPath;
        }

        public bool SaveConfig()
        {
            //try
            //{
            appConfig.SetTemplateFile(selectTemplateFileComboBox.Text);
            appConfig.SetListenPath(listenPathTextBox.Text);
            appConfig.SetOutputPath(outputPathTextBox.Text);

            okCallback(appConfig);

            return true;
            //}
            //catch (Exception e)
            //{
            //    MessageBox.Show(e.Message);
            //    return false;
            //}
        }

        public void ShowIt()
        {
            Show();
            parentForm.EnabledConfigButton(false);
        }

        public void CloseIt()
        {
            Close();
            parentForm.EnabledConfigButton(true);
            parentForm.Enabled = true;
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            if(SaveConfig())
            {
                CloseIt();
            }
        }

        private void listenPathTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            if(parentForm.xyjcApp == null)
            {
                if (SaveConfig())
                {
                    CloseIt();
                }
            }
            else
            {
                CloseIt();
            }
        }

        private void selectTemplateFileButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = false;
            fileDialog.Title = "请选择模板文件";
            fileDialog.Filter = "所有文件(*doc*)|*.doc*"; //设置要选择的文件的类型
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                templateFileTextBox.Text = fileDialog.FileName;
            }
        }

        private void selectListenPathButton_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择要监听的文件夹";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                if (string.IsNullOrEmpty(dialog.SelectedPath))
                {
                    MessageBox.Show(this, "文件夹路径不能为空", "提示");
                    return;
                }
                listenPathTextBox.Text = dialog.SelectedPath;
            }
        }

        private void selectOutputPathButton_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择输出文件夹";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                if (string.IsNullOrEmpty(dialog.SelectedPath))
                {
                    MessageBox.Show(this, "文件夹路径不能为空", "提示");
                    return;
                }
                outputPathTextBox.Text = dialog.SelectedPath;
            }
        }

        private void Form1Config_Load(object sender, EventArgs e)
        {

        }
    }
}
